#include "Tank.h"
#include "global.h"
// [Embed(source = '->->/assets/ice->png')] private starPNG:Class;
#include "Bullet.h"
#include "SimpleAudioEngine.h"
#define yuliang 0.5
void Tank::destory() 
{
 	//tank0
	bomb();
	//if (global_isBomb)
    {
		if (global_sound) {
            //todo SoundEngine.playEffect(Resource.s_buttonEffect);
            CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("SoundBoom.mp3");
        }
    }

	if(theid==0)
    {
		//global_tank0num = 0;
        global_myTank=NULL;
    }
	else
	{
		global_tank1num--;
		global_instance->showenemyNum();

	}
	if(this->getParent()!=NULL)
		this->getParent()->removeChild(this, true);
    
}

void Tank::gotoAndStop(int i) 
{
	this->initWithSpriteFrame((CCSpriteFrame *)this->fs->objectAtIndex(i - 1));
	frame = i;
}
void Tank::recttoids(CCRect r,std::vector<CCSprite *>)
{
	
}
bool Tank::onice()
{
    int i=floor(the.y/16);
	int j=floor(the.x/16);
    int t = map_map1[25-i][j];
    if(t==5) return true;
    else return false;
}
bool Tank::tryMoveUp(float dt)
{
	//if(dt>15) dt=15;
	//this->gotoAndStop(1);
    if(onice()) dt=dt*2;
	dir=1;
	this->setRotation(0);
        //float prexc=the.x;
        //float preyc=the.y;
	//CCPoint pre=this->getPosition();
	//float prey=the.y+this->getContentSize().height/2;
	//int newy=prey+dt;
	//CCRect chongtu=CCRectMake(pre.x-this->getContentSize().width/2,prey,this->getContentSize().width,dt);//rect
	//std::vector<CCSprite*> sps;
	int l=((the.x-this->getContentSize().width/2)/16);
    int rt=((the.x+this->getContentSize().width/2)/16);
	int b=floor((the.y+this->getContentSize().height/2)/16);//maybe <0,so must floor
	int top=floor((the.y+this->getContentSize().height/2+dt)/16);//maybe <0,so must floor
	if(rt>25) rt=25;
	for(int j=b;j<=top;j++)
	{
		if((25-j)>=0)
		{
			for(int i=l;i<=rt;i++)
			{
				if(!this->check(i,25-j))//
				{
					this->setPosition(ccp(the.x,j*16-this->getContentSize().height/2-yuliang));
					//sprintf(tmpbuffer,"conflict prey=%d,newy=%d,row=%d,col=%d",prey,newy,25-j,i);
					//CCLog(tmpbuffer);
                    //global_instance->showChongtu(chongtu,b,top,l,rt,j,i);
					return false;
				}
			}
		
		}
		else
		{
			this->setPosition(ccp(the.x,416-this->getContentSize().height/2-yuliang));
           // global_instance->showChongtu(chongtu,b,top,l,rt,j,l);
			return false;
		}
	}
	this->setPosition(ccp(the.x,the.y+dt));
	return true;
}

bool Tank::tryMoveDown(float dt) {
	//if(dt>15) dt=15;
	if(onice()) dt=dt*2;
	dir=2;
	this->setRotation(180);
	//CCPoint pre=this->getPosition();
        //float prexc=the.x;
        //float preyc=the.y;
	//float prey=the.y-this->getContentSize().height/2;
	//int newy=prey-dt;
	//CCRect chongtu=CCRectMake(prexc-this->getContentSize().width/2,prey-dt,this->getContentSize().width,dt);//rect
	//std::vector<CCSprite*> sps;
	int l=((the.x-this->getContentSize().width/2)/16);
	int b=floor((the.y-this->getContentSize().height/2-dt)/16);
	int rt=((the.x-this->getContentSize().width/2+this->getContentSize().width)/16);
	int top=floor((the.y-this->getContentSize().height/2)/16);
	if(rt>25) rt=25;
	for(int j=top;j>=b;j--)
	{
		if((25-j)<=25)
		{
			for(int i=l;i<=rt;i++)
			{
					if( !this->check(i,25-j))
					{
						this->setPosition(ccp(the.x,j*16+16+this->getContentSize().height/2+yuliang));
						//sprintf(tmpbuffer,"conflict prey=%d,newy=%d,row=%d,col=%d",prey,newy,25-j,i);
						//CCLog(tmpbuffer);
                        //global_instance->showChongtu(chongtu,b,top,l,rt,j,i);
						return false;
					}
			}
		}
		else
		{
			this->setPosition(ccp(the.x,yuliang+this->getContentSize().height/2));
            //global_instance->showChongtu(chongtu,b,top,l,rt,l,j);
			return false;
		}
	}
	this->setPosition(ccp(the.x,the.y-dt));
	return true;
}

bool Tank::tryMoveLeft(float dt) {
	if(onice()) dt=dt*2;
	dir=3;
	this->setRotation(-90);
	//this->gotoAndStop(3);
	//CCPoint pre=this->getPosition();
	//float prex=pre.x-this->getContentSize().width/2;
	//float newx=prex-dt;
	//CCRect chongtu=CCRectMake(newx,pre.y-this->getContentSize().height/2,dt,this->getContentSize().height);//rect
	//std::vector<CCSprite*> sps;
	int bottom=((the.y-this->getContentSize().height/2)/16);
	int left=floor((the.x-this->getContentSize().width/2-dt)/16);
	int right=floor((the.x-this->getContentSize().width/2)/16);
	int top=((the.y+this->getContentSize().height/2)/16);
	//int top=ceil(r.origin.y+r.size.height)/16;
	if(top>25) top=25;
	for(int j=right;j>=left;j--)
	{
            if(j>=0)
            {
                for(int i=bottom;i<=top;i++)
                {
                        
                    if(!this->check(j,25-i))//
                    {
                            this->setPosition(ccp(j*16+16+this->getContentSize().height/2+yuliang,the.y));
                            //sprintf(tmpbuffer,"conflict prex=%d,newx=%d,grid=%d",prex,newx,j);
                            //CCLog(tmpbuffer);
                            //global_instance->showChongtu(chongtu,bottom,top,left,right,i,j);
                            return false;
                    }
                }
            }
            else
            {
                    this->setPosition(ccp(yuliang+this->getContentSize().width/2,the.y));
                //global_instance->showChongtu(chongtu,bottom,top,left,right,bottom,j);
                    return false;
            }
	}
	this->setPosition(ccp(the.x-dt,the.y));
	return true;
}

bool Tank::tryMoveRight(float dt) {
	if(onice()) dt=dt*2;
	dir=4;
	//gotoAndStop(4);
	this->setRotation(90);
	//CCPoint pre=this->getPosition();
	//float prex=the.x+this->getContentSize().width/2;
	//int newx=prex+dt;
	//CCRect chongtu=CCRectMake(prex,pre.y-this->getContentSize().height/2,dt,this->getContentSize().height);//rect
	//std::vector<CCSprite*> sps;
	int bottom=((the.y-this->getContentSize().height/2)/16);
	int left=floor((the.x+this->getContentSize().width/2)/16);
	int right=floor((the.x+this->getContentSize().width/2+dt)/16);
	int top=((the.y+this->getContentSize().height/2)/16);
	//int top=ceil(r.origin.y+r.size.height)/16;
	if(top>25) top=25;
	for(int j=left;j<=right;j++)
	{
		if(j<=25)
		{
			for(int i=bottom;i<=top;i++)
			{
				if(!this->check(j,25-i))//
				{
					this->setPosition(ccp(j*16-this->getContentSize().width/2-yuliang,the.y));
					//sprintf(tmpbuffer,"conflict prex=%d,newx=%d,grid=%d",prex,newx,j);
					//CCLog(tmpbuffer);
                    //global_instance->showChongtu(chongtu,bottom,top,left,right,i,j);
					return false;
				}
			}
		
		}
		else
		{
            //global_instance->showChongtu(chongtu,bottom,top,left,right,bottom,j);
			this->setPosition(ccp(416-this->getContentSize().width/2-yuliang,the.y));
			//CCLog("right border");
			return false;
		}
	}
	//CCLog("no confilct");
	this->setPosition(ccp(the.x+dt,the.y));
	return true;
}

bool Tank::check(int col, int row) {
	if (map_map1[row][col] == 0 || map_map1[row][col] == 3
		|| map_map1[row][col] == 5)
		return true;
	else
		return false;
}


void Tank::shot() {
    
	Bullet *bullet = new Bullet(the.x,the.y, dir, theid, bulletSpeed);    bullet->autorelease();
	global_instance->bullet->addChild(bullet);
}


void Tank::bomb() {
	//float w = (this->getContentSize().width);
	//CCPoint the = this->getPosition();
	//float h = (this->getContentSize().height);
	BombFx* bomb = new BombFx(the.x , the.y, score);bomb->autorelease();
	if (this->isMe) {

		bomb->isme = true;
	} else {

	}
	global_instance->map->addChild(bomb);
}

bool Tank::checkHit()
{
	float thewidth=this->getContentSize().width;
	float theheight=this->getContentSize().height;
	//the=this->getPosition();

	for ( int j= 0; j < map_tankArr->count(); j++)
	{
		Tank *tank= (Tank *)map_tankArr->objectAtIndex(j);
		CCPoint  tankp=tank->getPosition();
		if (this == tank)
			continue;

		float h =tank->getContentSize().height;
		float w= tank->getContentSize().width;

		CCPoint p1a =ccp(the.x,the.y);
		CCPoint p1b= CCPointMake(p1a.x + thewidth, p1a.y + theheight);

		CCPoint p2a = tank->getPosition();
		CCPoint p2b = CCPointMake(p2a.x + w, p2a.y + h);

		if (global_hitRect(p1a, p1b, p2a, p2b))
		{
			switch (frame+1)
			{
			case 1:
				if (tankp.y + h < the.y)
					return true;
				break;
			case 2:
				if (tankp.y > the.y + theheight)
					return true;
				break;
			case 3:
				if (tankp.x + w < the.x)
					return true;
				break;
			case 4:
				if (tankp.x > the.x + thewidth)
					return true;
				break;
			}
		}

	}
	return false;
}

Tank::Tank(const char* filepath,float x,float y) {
	speed = 1;
	life = 1;
	dir = 1;
	score = 0;
	bulletSpeed = 6;
	isMe = false;
		frame=0;
		this->initWithSpriteFrameName(filepath);
		this->setPosition(ccp(x,y));
    the=this->getPosition();
}

